package com.vue.admin.service.common.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: vue-admin-service
 * @description: JSON 数据处理工具类
 * @author: zhangyangyang
 * @create: 2020-04-03 13:46
 **/
public class JsonUtils {

    private final static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * object转化json
     * @param obj
     * @return
     * @throws Exception
     */
    public static String beanToJson(Object obj) {
        try {
            return JsonUtils.objectMapper.writeValueAsString(obj);
        } catch(JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static ObjectMapper getInstance() {
        return JsonUtils.objectMapper;
    }

    /**
     * json转对象
     * @param jsonStr
     * @param clazz
     * @return
     * @throws Exception
     */
    public static <T> T jsonToBean(String jsonStr, Class<T> clazz) {
        try {
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return JsonUtils.objectMapper.readValue(jsonStr, clazz);
        } catch(JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json数组转list，指定类型
     * @param jsonArrayStr
     * @param clazz
     * @return
     * @throws Exception
     */
    public static <T> List<T> jsonToList(String jsonArrayStr, Class<T> clazz) throws Exception {
        JavaType javaType = getCollectionType(ArrayList.class, clazz);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        List<T> result = (List<T>) objectMapper.readValue(jsonArrayStr, javaType);
        return result;
    }
    /**
     * 获取泛型的Collection Type
     *
     * @param collectionClass 泛型的Collection
     * @param elementClasses  元素类
     * @return JavaType Java类型
     * @since 1.0
     */
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    /**
     * json转map
     * @param jsonStr
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static <T> Map<String, Object> jsonToMap(String jsonStr) throws Exception {
        return JsonUtils.objectMapper.readValue(jsonStr, Map.class);
    }

    /**
     * map转化对象
     */
    @SuppressWarnings("rawtypes")
    public static <T> T mapToBean(Map map, Class<T> clazz) {
        return JsonUtils.objectMapper.convertValue(map, clazz);
    }

    private JsonUtils() { }
}
